package com.graduation.books_borrow.Service.impl;

import com.graduation.books_borrow.Service.BooksBorrowService;
import com.graduation.books_borrow.entity.Book;
import com.graduation.books_borrow.entity.BooksBorrow;
import com.graduation.books_borrow.entity.User;
import com.graduation.books_borrow.repository.BooksBorrowMapper;
import com.graduation.books_borrow.repository.BooksQueryMapper;
import com.graduation.books_borrow.repository.UserMapper;
import com.graduation.books_borrow.ret.R;
import com.graduation.books_borrow.ret.Rcode;
import com.graduation.books_borrow.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BooksBorrowServiceImpl implements BooksBorrowService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BooksQueryMapper booksQueryMapper;

    @Autowired
    private BooksBorrowMapper booksBorrowMapper;

    @Override
    public R borrowBook(String userId, String bookId) {
        Map<String, String> errorRet = new HashMap<>();
        errorRet.put("userId", userId);
        errorRet.put("bookId", bookId);

        if (null == userId || "".equals(userId)) {
            return R.ret(Rcode.BOOK_BORROW_USERID_ERROR, Rcode.BOOK_BORROW_USERID_ERROR_DESC, errorRet);
        }
        User user = new User();
        user.setId(userId);
        User u = userMapper.selectUserByUserId(user);
        if (u == null) {
            return R.ret(Rcode.BOOK_BORROW_USERID_ERROR, Rcode.BOOK_BORROW_USERID_ERROR_DESC, errorRet);
        }

        if (null == bookId || "".equals(bookId)) {
            return R.ret(Rcode.BOOK_BORROW_BOOK_ID_ERROR, Rcode.BOOK_BORROW_BOOK_ID_ERROR_DESC, errorRet);
        }

        Book book = booksQueryMapper.booksQueryById(Integer.valueOf(bookId));
        if(book == null) {
            return R.ret(Rcode.BOOK_BORROW_BOOK_ID_ERROR, Rcode.BOOK_BORROW_BOOK_ID_ERROR_DESC, errorRet);
        }


        List<BooksBorrow> list = booksBorrowMapper.bookBorrowQueryByUserId(new BooksBorrow(userId));
        if(list != null && list.size() >= 3) {
            return R.ret(Rcode.BOOK_BORROW_OUT_THREE, Rcode.BOOK_BORROW_OUT_THREE_DESC, errorRet);
        }

        BooksBorrow booksBorrow = new BooksBorrow();
        booksBorrow.setBookId(bookId);
        booksBorrow.setStatus(1);
        BooksBorrow booksBorrow1 = booksBorrowMapper.bookBorrowQuery(booksBorrow);
        if (booksBorrow1 != null) {
            return R.ret(Rcode.BOOK_BORROW_HAS_BORROW, Rcode.BOOK_BORROW_HAS_BORROW_DESC, errorRet);
        }

        boolean ret = booksBorrowMapper.bookBorrow(new BooksBorrow(bookId, userId, 1, TimeUtil.getCurrentTimestamp()));
        if (ret) {
            return R.ret(Rcode.BOOK_BORROW_SUCCESS, Rcode.BOOK_BORROW_SUCCESS_DESC, errorRet);
        } else {
            return R.ret(Rcode.BOOK_BORROW_FAIL, Rcode.BOOK_BORROW_FAIL_DESC, errorRet);
        }

    }

    @Override
    public R returnBook(String userId, String bookId) {
        Map<String, String> errorRet = new HashMap<>();
        errorRet.put("userId", userId);
        errorRet.put("bookId", bookId);

        if (null == userId || "".equals(userId)) {
            return R.ret(Rcode.BOOK_RETURN_USERID_ERROR, Rcode.BOOK_RETURN_USERID_ERROR_DESC, errorRet);
        }
        User user = new User();
        user.setId(userId);
        User u = userMapper.selectUserByUserId(user);
        if (u == null) {
            return R.ret(Rcode.BOOK_RETURN_USERID_ERROR, Rcode.BOOK_RETURN_USERID_ERROR_DESC, errorRet);
        }

        if (null == bookId || "".equals(bookId)) {
            return R.ret(Rcode.BOOK_RETURN_BOOK_ID_ERROR, Rcode.BOOK_RETURN_BOOK_ID_ERROR_DESC, errorRet);
        }

        Book book = booksQueryMapper.booksQueryById(Integer.valueOf(bookId));
        if(book == null) {
            return R.ret(Rcode.BOOK_RETURN_BOOK_ID_ERROR, Rcode.BOOK_RETURN_BOOK_ID_ERROR_DESC, errorRet);
        }

        BooksBorrow booksBorrow = booksBorrowMapper.bookBorrowQueryByUserIdBookId(new BooksBorrow(bookId, userId));
        if (booksBorrow == null) {
            return R.ret(Rcode.BOOK_RETURN_BORROW_NOT_EXIST, Rcode.BOOK_RETURN_BORROW_NOT_EXIST_DESC, errorRet);
        }

        int ret = booksBorrowMapper.bookReturn(new BooksBorrow(bookId, userId));
        if (ret > 0) {
            return R.ret(Rcode.BOOK_RETURN_SUCCESS, Rcode.BOOK_RETURN_SUCCESS_DESC, errorRet);
        }

        return R.ret(Rcode.BOOK_RETURN_FAIL, Rcode.BOOK_RETURN_FAIL_DESC, errorRet);
    }
}
